// +----------------------------------------------------------------------
// | ILDP-three-dimensional系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.lyltop.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 彦麟工作室 <yanlinstudio@126.com>
// +----------------------------------------------------------------------
// | Name: 刘彦麟
// +----------------------------------------------------------------------
// | FILE_NAME: highlight.ts
// +----------------------------------------------------------------------
// | Date: 2022/6/14 09:43
// +----------------------------------------------------------------------

import {EffectComposer} from "three/examples/jsm/postprocessing/EffectComposer";
import {RenderPass} from "three/examples/jsm/postprocessing/RenderPass";
import {OutlinePass} from "three/examples/jsm/postprocessing/OutlinePass";
import * as THREE from "three";
import {ShaderPass} from "three/examples/jsm/postprocessing/ShaderPass";
import {FXAAShader} from "three/examples/jsm/shaders/FXAAShader";
import {Camera, Scene, WebGLRenderer} from "three";

/**
 * 点击建模物体，物体会高亮显示
 *
 * */
let effectComposer: EffectComposer | null = null;
let outlinePsaa = null;
let renderPass = null;
let effectFXAA = new ShaderPass(FXAAShader);
//高亮显示模型（呼吸灯）
export const OutlineObj = (renderer: WebGLRenderer, scene: Scene, camera: Camera, selectedObject: any): EffectComposer => {
    // 创建效果组合器对象，在该对象上添加后期处理通道
    effectComposer = new EffectComposer(renderer);
    // 新建一个场景通道，覆盖到原来的场景上
    renderPass = new RenderPass(scene, camera);
    effectComposer.addPass(renderPass);
    // 物体边缘发光通道
    outlinePsaa = new OutlinePass(
        new THREE.Vector2(window.innerWidth, window.innerHeight),
        scene,
        camera
    );
    // 发光的对象是个数组 坑
    outlinePsaa.selectedObjects = [selectedObject];
    // 边框亮度
    outlinePsaa.edgeStrength = 30.0;
    // 光晕[0,1]
    outlinePsaa.edgeGlow = 0;
    // 是否使用父级的材质
    outlinePsaa.usePatternTexture = false;
    // 边框宽度
    outlinePsaa.edgeThickness = 5.0;
    // 边框弯曲度
    outlinePsaa.downSampleRatio = 1;
    // 呼吸闪烁的速度
    outlinePsaa.pulsePeriod = 3;
    // 呼吸显示的颜色
    outlinePsaa.visibleEdgeColor.set(parseInt("0x00ff00"))
    // 呼吸消失的颜色
    outlinePsaa.hiddenEdgeColor = new THREE.Color(0, 0, 0);

    outlinePsaa.clear = true;
    effectComposer.addPass(outlinePsaa);

    // 自定义着色器通道
    effectFXAA.uniforms["resolution"].value.set(
        1 / window.innerWidth,
        1 / window.innerHeight
    );
    effectFXAA.renderToScreen = true;
    effectComposer.addPass(effectFXAA);

    return effectComposer
};
